package com.example.spingbootmybatis.controller;

import com.example.spingbootmybatis.mapper.UserMapper;
import com.example.spingbootmybatis.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/login")
    public String Login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model, HttpSession session){
        //具体的业务
        System.out.println(username);
        System.out.println(password);
        user user = userMapper.queryUserByNP(username, password);
        System.out.println(user);
        if (user!=null)
        {
            session.setAttribute("userwho",user.getUsername());
            session.setAttribute("userpwd",user.getPassword());
            //return "list1";
            return "redirect:/main.html";   //登陆成功后重定向到第一页
        }
        else {
            model.addAttribute("loginerror","用户名或者密码错误");
            return "index";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        System.out.println("logout");
        return "redirect:/index.html";
    }
}
